/*******************************************************************************
 *	                           bsp ram clear.c
 *******************************************************************************/
#include "bsp.h"
#include "mytype.h"
#include "stdint.h"

/*******************************************************************************
 * @brief  stack infomation
 *******************************************************************************/
#define VECT_ADDR 0x8000000
#define STACK_SIZE 0x400

/*******************************************************************************
 * @brief  fun
 *******************************************************************************/
void ram_clr(void);

/*******************************************************************************
 * @brief  var
 *******************************************************************************/
typedef struct
{
	uint32_t addr;
	uint32_t size;
} mem_tab_type;

const mem_tab_type mem_clr_tab[] = {
	{0x20000000, 0x20000},
	{0x20020000, 0x10000},
};

/*******************************************************************************
 * @brief  ram_clr
 * \param[in] none
 * \retval: none
 *******************************************************************************/
void ram_clr(void)
{
	register uint32_t stk_addr;
	int tab_cnt;

	// stack
	stk_addr = inpw(VECT_ADDR) - STACK_SIZE;

	// clear ram , skip stack
	for (tab_cnt = 0; tab_cnt < arr_len(mem_clr_tab); tab_cnt++)
	{
		// 起始、结束地址计算
		uint32_t base = mem_clr_tab[tab_cnt].addr;
		uint32_t end = base + mem_clr_tab[tab_cnt].size - 1;
		register uint32_t index;

		if (rang_check(stk_addr, base, end))
		{
			// 含堆栈段，跳过堆栈
			for (index = base; index < stk_addr; index += 4)
			{
				mem32(index) = 0;
			}

			for (index = stk_addr + STACK_SIZE; index < end; index += 4)
			{
				mem32(index) = 0;
			}
		}
		else
		{
			// 不含堆栈段，全部清除
			for (index = base; index < end; index += 4)
			{
				mem32(index) = 0;
			}
		}
	}

	NOP1();
}
